#import "ProvinceAndCity.h"
#import "RequestCreator.h"

@implementation ProvinceAndCity
@synthesize picker;
-(id) init{
	if (self = [super init]) {
		provincesArray = [[NSArray alloc] initWithObjects:
		@"黑龙江",@"吉林",@"辽宁",@"内蒙古",@"河北",@"河南",@"山东",@"山西",
		@"江苏",@"安徽",@"陕西",@"宁夏",@"甘肃",@"青海",@"湖北",@"湖南",
		@"浙江",@"江西",@"福建",@"贵州",@"四川",@"广东",@"广西",@"云南",
		@"海南",@"新疆",@"西藏",@"台湾",@"北京",@"上海",@"天津",@"重庆",@"香港",@"澳门",nil];
		cityArray = [[NSMutableArray alloc] init];
		responseData = [[NSMutableData alloc] init];
	}
	return self;
}

#pragma mark pickerView datasource methods
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
	return 2;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
	if (component == 0) {
		return [provincesArray count];
	}else {
		return [cityArray count];
	}
}

#pragma mark pickerView delegate methods
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
	if (component == 0) {
		return [provincesArray objectAtIndex: row];
	}else {
		if ([cityArray count] > row) {
			return [cityArray objectAtIndex: row];
		}
		return nil;
	}
}

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
	if (component == 0) {
		NSString *envelope = [RequestCreator createGetCity: [provincesArray objectAtIndex: row]];
		[RequestCreator invokeWs : RequestGetCity envelope: envelope delegate: self];	
	}
}

#pragma mark self methods
-(void) selectPrefer{
	[picker selectRow: 0 inComponent:0 animated:YES];
	NSString *envelope = [RequestCreator createGetCity: [provincesArray objectAtIndex: 0]];
	[RequestCreator invokeWs: RequestGetCity  envelope: envelope delegate: self];	
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
	[responseData appendData:data];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData: responseData];
	[parser setDelegate: self];
	[parser parse];
}

-(void) saveCurrent{
	NSUInteger selectProvinceRow = [picker selectedRowInComponent: 0];
	NSUInteger selectCityRow = [picker selectedRowInComponent: 1];
	if (selectCityRow < 0 || selectProvinceRow < 0) {
		return ;
	}
	NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
	NSArray *previous = [ud arrayForKey: kPreferCityArray];
	
	NSMutableSet *set = [[NSMutableSet alloc] initWithArray : previous];
	NSString *value = [[NSString alloc] initWithFormat: @"%@_%@", [provincesArray objectAtIndex: selectProvinceRow], [cityArray objectAtIndex: selectCityRow]];
	[set addObject:value];
	[value release];
	NSArray *array = [set allObjects];
	
	[ud setObject: array forKey: kPreferCityArray];
	[ud synchronize];

	[set release];
}
#pragma mark xml parser methods

-(void) parserDidStartDocument:(NSXMLParser *)parser{
	[cityArray removeAllObjects]; 
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	NSArray *parts = [string componentsSeparatedByString: @","];
	[cityArray addObject: [parts objectAtIndex:0]];
}

-(void) parserDidEndDocument:(NSXMLParser *)parser{
	[parser release];
	[picker reloadComponent:1];
	[responseData release];
	responseData = [[NSMutableData alloc] init];
	[picker setUserInteractionEnabled: YES];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"error" message: [error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alert show];
	[alert release];
}
#pragma mark  dealloc
-(void)dealloc{
	[cityArray release];
	[provincesArray release];
	[responseData release];
	[picker release];
	[super dealloc];
}
@end
